package com.taylor.nio;

import org.testng.annotations.Test;

import java.nio.ByteBuffer;

/**
 * @Desc : 只读buffer
 *  将一个普通 Buffer 转成只读 Buffer
 * @Author ： taylor
 * @Date ：2021/6/26 18:34
 */
public class ReadOnlyBuffer {
    @Test
    public void test(){
        //创建一个 buffer
        ByteBuffer buffer = ByteBuffer.allocate(64);
        for (int i = 0; i < 64; i++) {
            buffer.put((byte) i);
        }
        //读取
        buffer.flip();
        //得到一个只读的 Buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        System.out.println(readOnlyBuffer.getClass());

        //读取
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println(readOnlyBuffer.get());
        }
        //ReadOnlyBufferException
        readOnlyBuffer.put((byte) 100);

    }
}
